# feker插入100条数据
import pymysql
from faker import Faker


def insert_user():
    #连接对象
    conn = pymysql.connections.Connection(host="127.0.0.1",port=3306,user="root",passwd="root",db="shop",charset="utf8")
    #通过连接对象，获取游标对象
    cursor = conn.cursor()
    faker = Faker(locale='zh_CN')
    for i in range(100):
        username = faker.user_name()
        password = faker.password()
        email = faker.email()
        phone = faker.phone_number()
        #定义sql
        sql = "insert into user (username,password,email,phone) values(%s,%s,%s,%s);"
        #执行sql
        cursor.execute(sql,(username,password,email,phone))
        print("已插入 {i} 条数据".format(i=i+1))
    #提交事务
    conn.commit()
    #释放资源
    cursor.close()
    conn.close()
if __name__ == '__main__':
    insert_user()